/*
Written by Best Flash Solutions, Inc  
See:
	http://www.bestflashsolutions.com
Contact me: contact@bestflashsolutions.com
*/

/**
 * Store movie configuration
 */
package com.bestflashsolutions.red5Streaming
{
	import flash.display.BitmapData;
	import flash.media.Camera;
	import flash.media.Microphone;
	
	import mx.controls.Alert;
	import mx.utils.UIDUtil;
	
	public class MovieSetting {
		//Name of video device 
		public var videoDevice: String = "";
		//Video bandwidth
		public var bandwidth: int = 0;
		//video width
		public var width: int = 0;
		//video height
		public var height: int = 0;
		//video quality
		public var quality: int = 0;
		//camera keyframe
		public var keyframe: int = 0
		//frame per second
		public var fps: int = 0;
		public var maxDuration: uint = 200000; //default 200s
		//Audio device name
		public var audioDevice: String = "";
		//audio frequency
		public var khz: int = 0;
		//audio silence
		public var silence: int = 0;
		//audio timeout
		public var timeout: int = 0;
		//audio recording volume
		public var volume: int = 0;
		//audio duration
		public var duration: Number = 0;
		//microphone echo suppression
		public var echoSuppression: Boolean = false;
		//microphone loopback
		public var loopBack: Boolean = false;
		
		public var objCamera: Camera = null;
		public var objMicrophone: Microphone = null;
		//public var filename: String;
		//public var thumbnailName: String;
		public var RTMPServer: String;
		public var streamName: String;
		public var appName: String = "webVideoConference";
		//public var bmpThumbnailData: BitmapData = null;
		
		public function MovieSetting()
		{
			//var s: String;
			//s = UIDUtil.createUID();
			//filename = "CamToFLV-" + s;
			//thumbnailName = "thumbnail-" + s + ".jpg";
			
			setDefault();			
		}
		
		//Default setting
		public function setDefault(): void {
			bandwidth = 30;
			width = 320;
			height = 240;
			quality = 0;
			keyframe = 5; 
			fps = 10;
			
			khz = 8;
			silence = 10;
			timeout = 2000;
			volume = 50;
			
			echoSuppression = true;
			loopBack = true;
			videoDevice = "";
			audioDevice = "";
		}
		
		//Update setting
		public function update(): void {
			getCameraHandler();
			getMicrophoneHandler();			
		}
		
		private function getCameraHandler():void{
			var camera: Camera = null;
			var s: String;
			//Find video device
			if (videoDevice!="") {
				for (var i:int =0; i< Camera.names.length; i++) {
					if (Camera.names[i]==videoDevice) {
						s = String(i);					
						camera = Camera.getCamera(s);
						break;
					}
				}
				objCamera = camera;
			}
			else
				objCamera = Camera.getCamera();
			
			
			if (objCamera) {
				objCamera.setKeyFrameInterval(keyframe);
				objCamera.setMode(width, height, fps);
				objCamera.setQuality(bandwidth*1024, quality); 
			}
		}
		
		private function getMicrophoneHandler():void{
			//Find microphone
			objMicrophone = null;
			if (audioDevice!="") {
				for (var i:int =0; i< Microphone.names.length; i++) {
					if (Microphone.names[i]== audioDevice) {
						try{
							objMicrophone = Microphone.getEnhancedMicrophone(i);
						}catch(err:Error){
							objMicrophone = Microphone.getMicrophone(i);
						}
						objMicrophone.gain = volume;
						objMicrophone.rate = khz;
						objMicrophone.setSilenceLevel(silence, timeout);
						objMicrophone.setUseEchoSuppression(echoSuppression);
						//Alert.show(echoSuppression ? "EchoSuppression=true" : "EchoSuppression=false");
						objMicrophone.setLoopBack(loopBack);
						//Alert.show(loopBack ? "LoopBack=true" : "Loopback=false");
						break;
					}
				}
			}
			else
				objMicrophone = Microphone.getMicrophone();
		}
		
		public function updateDeviceProperties(command:String):void{
			if(command == "audiovolume")
				objMicrophone.gain = volume;
			else if(command == "echosuppress")
				objMicrophone.setUseEchoSuppression(echoSuppression);
			else if(command == "movloopback")
				objMicrophone.setLoopBack(loopBack);
			else if(command == "videodevice")
				getCameraHandler();
			else if(command == "audiodevice")
				getMicrophoneHandler();
			else
				trace(command);
		}
		
		//Name of red5 stream
		public function getStreamName(): String {
			return streamName;
		}
		
		//Name of red5 application
		public function getAppName(): String {
			return appName;
		}
	}
}